package com.school.gmz.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school.gmz.common.Result;
import com.school.gmz.entity.StuFile;
import com.school.gmz.mapper.StuFileMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/stufile")
@Api(tags = "学生档案管理") // 定义API的分类
public class StuFileController {
    @Resource
    StuFileMapper stufileMapper;

    // 添加学生档案
    @PostMapping
    @ApiOperation(value = "添加学生档案", notes = "创建一个新的学生档案记录")
    public Result<?> save(@RequestBody StuFile stufile){
        stufileMapper.insert(stufile);
        return Result.success();
    }

    // 更新学生档案
    @PutMapping
    @ApiOperation(value = "更新学生档案", notes = "根据ID更新学生档案记录")
    public Result<?> update(@RequestBody StuFile stufile){
        stufileMapper.updateById(stufile);
        return Result.success();
    }

    // 删除学生档案
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除学生档案", notes = "根据ID删除学生档案记录")
    public Result<?> delete(@ApiParam(value = "学生档案ID", required = true) @PathVariable Long id){
        stufileMapper.deleteById(id);
        return Result.success();
    }

    // 查询学生档案
    @GetMapping
    @ApiOperation(value = "查询学生档案", notes = "支持姓名、性别、出生年份、手机号、班级、年级查询")
    public Result<?> findPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam(value = "姓名", defaultValue = "") @RequestParam(defaultValue = "") String name,
            @ApiParam(value = "性别", defaultValue = "") @RequestParam(defaultValue = "") String sex,
            @ApiParam(value = "出生年份", defaultValue = "") @RequestParam(defaultValue = "") String date,
            @ApiParam(value = "手机号", defaultValue = "") @RequestParam(defaultValue = "") String phone,
            @ApiParam(value = "班级", defaultValue = "") @RequestParam(defaultValue = "") String classes,
            @ApiParam(value = "年级", defaultValue = "") @RequestParam(defaultValue = "") String grade){
        LambdaQueryWrapper<StuFile> wrapper = Wrappers.<StuFile>lambdaQuery();
        if (StrUtil.isNotBlank(name)){
            wrapper.like(StuFile::getName, name);
        }
        if (StrUtil.isNotBlank(sex)){
            wrapper.eq(StuFile::getSex, sex);
        }
        if (StrUtil.isNotBlank(date)){
            wrapper.eq(StuFile::getDate, date);
        }
        if (StrUtil.isNotBlank(phone)){
            wrapper.eq(StuFile::getPhone, phone);
        }
        if (StrUtil.isNotBlank(classes)){
            wrapper.like(StuFile::getClasses, classes);
        }
        if (StrUtil.isNotBlank(grade)){
            wrapper.like(StuFile::getGrade, grade);
        }
        Page<StuFile> stuFilePage = stufileMapper.selectPage(new Page<>(pageNum, pageSize), wrapper); // 默认分页参数
        return Result.success(stuFilePage);
    }

    // 根据ID查询学生档案
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询学生档案", notes = "根据学生档案ID查询学生档案记录")
    public Result<?> findById(@ApiParam(value = "学生档案ID", required = true) @PathVariable Long id){
        StuFile stuFile = stufileMapper.selectById(id);
        if (stuFile != null) {
            return Result.success(stuFile);
        } else {
            return Result.failure("学生档案不存在");
        }
    }
}